<!--
  Copyright 2016 The LUCI Authors. All rights reserved.
  Use of this source code is governed under the Apache License, Version 2.0
  that can be found in the LICENSE file.
  -->

<link rel="import" href="../rpc/rpc-error.html">

<script>
  "use strict";

  function LogDogError(base) {
    this.base = base;
  };
  LogDogError.wrapGrpc = function(err) {
    if (err instanceof luci.rpc.GrpcError) {
      return new LogDogError(err);
    }
    return err;
  }

  LogDogError.prototype = Object.create(Error.prototype);
  LogDogError.prototype.isGrpcError = function() {
    return (this.base.name === "GrpcError");
  };
  LogDogError.prototype.isNotFound = function() {
    return (this.isGrpcError() && this.base.code === luci.rpc.Code.NOT_FOUND);
  };
  LogDogError.prototype.isUnauthenticated = function() {
    return (this.isGrpcError() &&
        this.base.code === luci.rpc.Code.UNAUTHENTICATED);
  };
  LogDogError.prototype.isTransient = function() {
    if ( ! this.isGrpcError() ) {
      return false;
    }
    switch ( this.base.code ) {
    case luci.rpc.Code.INTERNAL:
    case luci.rpc.Code.UNAVAILABLE:
    case luci.rpc.Code.RESOURCE_EXHAUSTED:
      return true;

    default:
      return false;
    }
  };
</script>
